<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Forpastime < forpastime@qq.com>
// +----------------------------------------------------------------------
namespace app\tenant\controller;

use cmf\controller\AdminBaseController;
use app\admin\model\AdminMenuModel;
use app\tenant\model\ShopInfoModel;
use think\Db;

/**
 * Class AdminIndexController
 * @package app\tenant\controller
 * @adminMenuRoot(
 *     'name'   =>'多商户管理',
 *     'action' =>'default',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 30,
 *     'icon'   =>'bank',
 *     'remark' =>'多商户入驻管理'
 * )
 */
class AdminIndexController extends AdminBaseController
{

    public function initialize()
    {
        $adminSettings = cmf_get_option('admin_settings');
        if (empty($adminSettings['admin_password']) || $this->request->path() == $adminSettings['admin_password']) {
            $adminId = cmf_get_current_admin_id();
            if (empty($adminId)) {
                session("__LOGIN_BY_CMF_ADMIN_PW__", 1);//设置后台登录加密码
            }
        }

        parent::initialize();
    }

    /**
     * 后台首页
     */
    public function index()
    {
        $content = hook_one('admin_index_index_view');

        if (!empty($content)) {
            return $content;
        }

        $adminMenuModel = new AdminMenuModel();
        $menus          = cache('admin_menus_' . cmf_get_current_admin_id(), '', null, 'admin_menus');

        if (empty($menus)) {
            $menus = $adminMenuModel->menuTree();
            cache('admin_menus_' . cmf_get_current_admin_id(), $menus, null, 'admin_menus');
        }

        $this->assign("menus", $menus);
        $site_info          = cmf_get_site_info();
        $this->assign('site_info', $site_info);

        $result = Db::name('AdminMenu')->order(["app" => "ASC", "controller" => "ASC", "action" => "ASC"])->select();
        $menusTmp = array();
        foreach ($result as $item){
            //去掉/ _ 全部小写。作为索引。
            $indexTmp = $item['app'].$item['controller'].$item['action'];
            $indexTmp = preg_replace("/[\\/|_]/","",$indexTmp);
            $indexTmp = strtolower($indexTmp);
            $menusTmp[$indexTmp] = $item;
        }
        $this->assign("menus_js_var",json_encode($menusTmp));

        //$admin = Db::name("user")->where('id', cmf_get_current_admin_id())->find();
        //$this->assign('admin', $admin);
        return $this->fetch();
    }

    /**
     * 商户管理
     * @adminMenu(
     *     'name'   => '商户信息',
     *     'parent' => 'tenant/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '商户信息',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function info()
    {
        $admin_id = cmf_get_current_admin_id();
        $admin = DB::name('user')->where('id',$admin_id)->find();

        $tenant_id = $admin['tenant_id'] !=0 ?$admin['tenant_id']:$admin_id;

        $shop_info_id = Db::name('shop_apply_order')->where('user_id',$tenant_id)->value('shop_info_id');
        
        $shopInfoModel = new ShopInfoModel();
        $shop  = $shopInfoModel->getOne($shop_info_id);

        $shop_service = !empty($shop['services'])?$shop['services']:[];
        $shop_business = !empty($shop['business'])?$shop['business']:[];


        $service = Db::name('tenant_service')->where('status',1)->select();
        $business = Db::name('tenant_business_scope')->where('status',1)->select();

        $this->assign('shop',$shop);
        $this->assign('service',$service);
        $this->assign('business',$business);

        $this->assign('shop_service',!empty($shop['services'])?json_decode($shop_service):[]);
        $this->assign('shop_business',!empty($shop['business'])?json_decode($shop_business):[]);

        return $this->fetch();
    }

    /**
     * 商户管理
     * @adminMenu(
     *     'name'   => '商户信息修改',
     *     'parent' => 'info',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '商户信息修改',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function updateInfo()
    {
        $data = $this->request->param('post');
       
        $shopimages_urls = $this->request->param('shopimages_urls');
            $shopimages='';
            if(!empty($shopimages_urls)){
                foreach($shopimages_urls as $k=> $v){
                    if($k!=0){
                        $shopimages = $shopimages.','. cmf_get_image_preview_url($v);

                    }else{
                        $shopimages = cmf_get_image_preview_url($v);

                    }
                }
                $data['shop_images'] = $shopimages;
            
            }
        $cert_urls = $this->request->param('cert_urls');
            $certificates='';
            if(!empty($cert_urls)){
                foreach($cert_urls as $k=> $v){
                    if($k!=0){
                        $certificates = $certificates.','. cmf_get_image_preview_url($v);

                    }else{
                        $certificates = cmf_get_image_preview_url($v);

                    }
                }
                $data['other_certificate'] = $certificates;
            
            }
        $service = $this->request->param('service');

        if(!empty($service)){
            $data['services'] = json_encode($service);
        }
        $business = $this->request->param('business');

        if(!empty($business)){
            $data['business'] = json_encode($business);
        }
        $res = Db::name("shop_info")
            ->where('id',$data['id'])->update($data);
        if($res){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');

        }
        
    }

    /**
     * 商户关店
     * @adminMenu(
     *     'name'   => '商户关店',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '商户关店',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function close()
    {
        $param = $this->request->param();
        if(!empty($param)){
            $msg = $param['msg'];
            $id = $param['id'];
            $res = Db::name("shop_info")->where('id',$id)->update(['check_msg'=>$msg,'status'=>0]);
            if($res){
                $this->success('关闭成功');
            }else{
                $this->error('关闭失败');

            }
        }
    }
    /**
     * 商户恢复开店
     * @adminMenu(
     *     'name'   => '商户恢复开店',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 20,
     *     'icon'   => '',
     *     'remark' => '商户恢复开店',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function reOpen()
    {
        $param = $this->request->param();
        if(!empty($param)){
            $id = $param['id'];
            $res = Db::name("shop_info")->where('id',$id)->update(['status'=>1]);
            if($res){
                $this->success('恢复成功');
            }else{
                $this->error('恢复失败');
            }
        }
    }
}
